package Servlet;

import Bean.User;
import Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 11:58
 */
//用户登录
@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        User user = new User();
        user.setPhone(req.getParameter("phone"));
        user.setPassword(req.getParameter("password"));
        User loginUser = userService.Login(user);
        HttpSession session = req.getSession();
        if(loginUser == null) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "用户不存在!!");
            req.setAttribute("redirectUrl", "login.jsp");
            req.setAttribute("waitSeconds", 2);
            session.setAttribute("LoginUserId",null);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        //在session中存储登录用户的userId

        session.setAttribute("LoginUser",loginUser);
        session.setAttribute("LoginUserId",loginUser.getUserId());

        session.setMaxInactiveInterval(60 * 60);
        req.getRequestDispatcher("user_index.jsp").forward(req,resp);
    }
}
